import 'dart:io';

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper_app/db/love_database.dart';
import 'package:wallpaper_app/http/dio_instance.dart';
import 'package:wallpaper_app/pages/main_page.dart';
import 'package:wallpaper_app/proxy/my_http_overrides.dart';
import 'package:wallpaper_app/themes/localization_info.dart';
import 'package:wallpaper_app/themes/theme_provider.dart';
import 'package:window_manager/window_manager.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await LoveDatabase.initialize();
  // dio初始化
  DioInstance.instance().initDio(baseUrl: '');
  DioInstance.instance().addResponseInterceptor();

  HttpOverrides.global = MyHttpOverrides();

  doWhenWindowReady(() {
    const initialSize = Size(900, 650);
    const minSize = Size(400, 300);
    appWindow.minSize = minSize;
    appWindow.size = initialSize;
    appWindow.alignment = Alignment.center;
    appWindow.title = '波奇壁纸';
    appWindow.show();
  });

  await windowManager.ensureInitialized();

  PaintingBinding.instance.imageCache.maximumSize = 2000; // 设置缓存数量
  PaintingBinding.instance.imageCache.maximumSizeBytes =
      300 << 20; // 设置缓存大小为 300 MB

  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => ThemeProvider()),
      ChangeNotifierProvider(create: (contex) => LoveDatabase()),
      ChangeNotifierProvider(create: (context) => LocalizationInfo()), // 新增
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return Consumer<LocalizationInfo>(builder: (context, provider, child) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        locale: const Locale('zh', 'CH'),
        localizationsDelegates: const [
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: const [
          Locale('zh', 'CH'),
          Locale('en', 'US'),
        ],
        title: '波奇壁纸',
        theme: Provider.of<ThemeProvider>(context).themeData,
        builder: BotToastInit(), //1. call BotToastInit
        navigatorObservers: [
          BotToastNavigatorObserver()
        ], //2. registered route observer
        home: MainPage(),
        routes: {},
      );
    });
    
  }
}
